# -*- encoding: utf-8 -*-
'''
@File    :   find.py
@Time    :   2021/12/27 01:29:56
@Author  :   January.Plus 
@Wechat  :   cjy4959
'''
# here put the import lib

import os

def hierar_search_file(relative_path:str,cwd:str)->str:
    if os.path.exists(relative_path):
        return os.path.abspath(relative_path)

    if os.path.isabs(relative_path):
        print("not relative path")
        exit(1)
        
    if not os.path.isdir(cwd):
        print("cwd is not directory")
        exit(1)
    
    dir = cwd
    while True:
        if os.path.exists(os.path.join(dir,relative_path)):
            return os.path.abspath(os.path.join(dir,relative_path))
        if dir == '':
            print(f"not find {relative_path}")
            exit(1)
        dir = os.path.dirname(dir)
        
if __name__ == '__main__':
    ret = hierar_search_file(
        relative_path="GraphEmbedding/data/bash/transitive"
        ,cwd="/Users/plus/Desktop/module-reverse-by-gnn/GraphEmbedding/examples"
    )
    
    print(ret)